package io.github.stary1993.kite.admin.controller;

import cloud.tianai.captcha.common.response.ApiResponse;
import cn.hutool.core.util.StrUtil;
import io.github.stary1993.kite.captcha.support.behavior.BehaviorImageCaptcha;
import io.github.stary1993.kite.captcha.support.behavior.BehaviorImageCaptchaTrack;
import io.github.stary1993.kite.captcha.support.simple.SimpleImageCaptcha;
import io.github.stary1993.kite.captcha.support.simple.SimpleImageCaptchaType;
import io.github.stary1993.kite.captcha.vo.BehaviorCaptchaVO;
import io.github.stary1993.kite.captcha.vo.CaptchaResponse;
import io.github.stary1993.kite.captcha.vo.SimpleCaptchaVO;
import io.github.stary1993.kite.core.util.R;
import io.github.stary1993.kite.syslog.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@Tag(name = "验证码模块")
@RequiredArgsConstructor
@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    private final SimpleImageCaptcha simpleImageCaptcha;

    private final BehaviorImageCaptcha behaviorImageCaptcha;

    @SysLog(value = "生成简单图片验证码", operatorType = "GENERATE_CAPTCHA")
    @Operation(summary = "生成简单图片验证码")
    @Parameters(value = {
            @Parameter(name = "type", description = "验证码类型"),
            @Parameter(name = "format", description = "数据格式", example = "json"),
            @Parameter(name = "timestamp", description = "时间戳")
    })
    @SneakyThrows
    @GetMapping("/simple/gen")
    public R<CaptchaResponse<SimpleCaptchaVO>> genSimpleImageCaptcha(HttpServletResponse response, @RequestParam("type") String type, String format, String timestamp) {
        if (StrUtil.isNotBlank(format) && StrUtil.equals(format, "json")) {
            return R.ok(simpleImageCaptcha.generate(SimpleImageCaptchaType.valueOf(type), timestamp));
        } else {
            simpleImageCaptcha.out(SimpleImageCaptchaType.valueOf(type), timestamp, response.getOutputStream());
        }
        return null;
    }

    @SysLog(value = "校验简单图片验证码", operatorType = "CHECK_CAPTCHA")
    @Operation(summary = "校验简单图片验证码")
    @Parameters(value = {
            @Parameter(name = "id", description = "验证码ID"),
            @Parameter(name = "type", description = "验证码类型"),
            @Parameter(name = "timestamp", description = "时间戳"),
            @Parameter(name = "code", description = "验证码")
    })
    @PostMapping("/simple/check")
    public R<ApiResponse> checkSimpleImageCaptcha(@RequestParam(value = "id", required = false) String id,
                                              @RequestParam("type") String type,
                                              @RequestParam(value = "timestamp", required = false) String timestamp,
                                              @RequestParam("code") String code) {
        if (id != null) {
            return R.ok(simpleImageCaptcha.matching(id, SimpleImageCaptchaType.valueOf(type), timestamp, code));
        } else {
            return R.ok(simpleImageCaptcha.matching(SimpleImageCaptchaType.valueOf(type), timestamp, code));
        }
    }


    @SysLog(value = "生成行为图片验证码", operatorType = "GENERATE_CAPTCHA")
    @Operation(summary = "生成行为图片验证码")
    @Parameter(name = "type", description = "验证码类型")
    @GetMapping("/behavior/gen")
    public R<CaptchaResponse<BehaviorCaptchaVO>> genBehaviorImageCaptcha(@RequestParam("type") String type) {
        return R.ok(behaviorImageCaptcha.generate(type));
    }

    @SysLog(value = "校验行为图片验证码", operatorType = "CHECK_CAPTCHA")
    @Operation(summary = "校验行为图片验证码")
    @Parameters(value = {
            @Parameter(name = "id", description = "验证码ID"),
            @Parameter(name = "imageCaptchaTrack", description = "行为图片验证码轨迹")
    })
    @PostMapping("/behavior/check")
    public R<ApiResponse> checkBehaviorImageCaptcha(@RequestParam("id") String id, @RequestBody BehaviorImageCaptchaTrack imageCaptchaTrack) {
        return R.ok(behaviorImageCaptcha.matching(id, imageCaptchaTrack));
    }

}
